//时间比较 13:00:00

export const timeCompare = (time1, time2) => {
  if (!time1) {
    return
  }
  let time1arr = (time1 && time1.split(':')) || []
  let time2arr = (time2 && time2.split(':')) || []
  let time1addDate = new Date()
  let time2addDate = new Date()
  if (time1arr[0]) {
    time1addDate.setHours(time1arr[0])
  }
  if (time1arr[1]) {
    time1addDate.setMinutes(time1arr[1])
  }
  if (time1arr[2]) {
    time1addDate.setSeconds(time1arr[2])
  }
  if (time2arr[0]) {
    time2addDate.setHours(time2arr[0])
  }
  if (time2arr[1]) {
    time2addDate.setMinutes(time2arr[1])
  }
  if (time2arr[2]) {
    time2addDate.setSeconds(time2arr[2])
  }
  return time1addDate.getTime() - time2addDate.getTime()
}

//日期比较
export const dateCompare = (time1, time2) => {
  let time1Long = new Date(...getDateArr(time1))
  let time2Long
  if (time2) {
    time2Long = new Date(...getDateArr(time2))
  } else {
    time2Long = new Date().getTime()
  }
  return time1Long - time2Long
}

//日期格式化
// 栗子
// 1. (YY)YY-MM-DD hh:mm:ss => (20)19-10-04 10:02:02
// 2. (YY)YY年MM月DD日 hh时mm分ss秒 => (20)19年10月04日 10时02分02秒
export const formatDate = (oDate, sFormation) => {
  let date
  if (!oDate) {
    date = new Date()
  } else if (/^\d+$/.test(oDate)) {
    date = new Date(oDate)
  } else {
    date = new Date(...getDateArr(oDate))
  }
  let YYYY = date.getFullYear() + ''
  let YY = YYYY && YYYY.substring(2)
  let MM = date.getMonth() + 1
  let DD = date.getDate()
  let hh = date.getHours()
  let mm = date.getMinutes()
  let ss = date.getSeconds()
  let dateall = {
    YYYY: YYYY,
    YY: YY,
    MM: MM > 9 ? MM : '0' + MM,
    DD: DD > 9 ? DD : '0' + DD,
    hh: hh > 9 ? hh : '0' + hh,
    mm: mm > 9 ? mm : '0' + mm,
    ss: ss > 9 ? ss : '0' + ss
  }
  if (sFormation) {
    let timeStr = ''
    let reDate = /((YYYY)|(YY))?([^YMDhms]+)?(MM)?([^YMDhms]+)?(DD)?([^YMDhms]+)?(hh)?([^YMDhms]+)?(mm)?([^YMDhms]+)?(ss)?([^YMDhms]+)?/
    let year = ''
    let year_unit = ''
    let month = ''
    let month_unit = ''
    let day = ''
    let day_unit = ''
    let hour = ''
    let hour_unit = ''
    let min = ''
    let min_unit = ''
    let sec = ''
    let sec_unit = ''

    sFormation.replace(reDate, function(
      $0,
      $1,
      $2,
      $3,
      $4,
      $5,
      $6,
      $7,
      $8,
      $9,
      $10,
      $11,
      $12,
      $13,
      $14
    ) {
      year = ($1 && dateall[$1]) || ''
      year_unit = $4 || ''
      month = ($5 && dateall[$5]) || ''
      month_unit = $6 || ''
      day = ($7 && dateall[$7]) || ''
      day_unit = $8 || ''
      hour = ($9 && dateall[$9]) || ''
      hour_unit = $10 || ''
      min = ($11 && dateall[$11]) || ''
      min_unit = $12 || ''
      sec = ($13 && dateall[$13]) || ''
      sec_unit = $14 || ''
      return ''
    })

    timeStr = `${year}${year_unit}${month}${month_unit}${day}${day_unit}${hour}${hour_unit}${min}${min_unit}${sec}${sec_unit}`
    return timeStr
  } else {
    return dateall
  }
}

//获取时间戳
//
export const getTimeLong = time => {
  let re = /^\d+$/
  if (re.test(time)) {
    return time
  } else {
    let dateArr = getDateArr(time) || []
    let timeLong = new Date(...dateArr)
    return timeLong
  }
}

/**
 * 输入Unix时间戳，返回指定时间格式
 */
export const calcTimeHeader = (time, showHM) => {
  // 格式化传入时间
  const date = new Date(parseInt(time))
  const year = date.getUTCFullYear()
  const month = date.getUTCMonth()
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getUTCMinutes()
  // 获取当前时间
  const currentDate = new Date()
  const currentYear = date.getUTCFullYear()
  const currentMonth = date.getUTCMonth()
  const currentDay = currentDate.getDate()
  // 计算是否是同一天
  if (currentYear == year && currentMonth == month && currentDay == day) {
    // 同一天直接返回
    if (hour > 12) {
      return `下午 ${hour}:${minute < 10 ? '0' + minute : minute}`
    } else {
      return `上午 ${hour}:${minute < 10 ? '0' + minute : minute}`
    }
  }
  // 计算是否是昨天
  const yesterday = new Date(currentDate - 24 * 3600 * 1000)
  if (
    year == yesterday.getUTCFullYear() &&
    month == yesterday.getUTCMonth &&
    day == yesterday.getDate()
  ) {
    // 昨天
    return `昨天 ${hour}:${minute < 10 ? '0' + minute : minute}`
  } else {
    return `${month + 1}月${day}日${showHM ? ' ' + hour + ':' + minute : ''}`
  }
}

//获取日期数组 2018-10-22 10:23:22 / 2018/10/22 10:23:22  [2018,10,22,10,23,22]
const getDateArr = datetime => {
  if (!datetime) {
    return []
  }
  let timearr = datetime.split(' ')
  let date = timearr[0]
  let time = timearr[1]
  let returnArr = []
  let dateArr = []
  let timeArr = (time && time.split(':')) || []
  if (/-/g.test(date)) {
    //2018-1-1格式
    dateArr = date.split('-')
  } else if (/\//g.test(date)) {
    //2018/1/1格式
    dateArr = date.split('/')
  }
  dateArr[1] = dateArr[1] - 1
  returnArr = dateArr.concat(timeArr)
  return returnArr
}

// 格式化时间
export const formatTime = time => {
  if (time) {
    return time.replace(/-/g, '/')
  } else {
    return null
  }
}

// im日期格式转换
export const getDateDiff = dateTimeStamp => {
  let result
  let minute = 1000 * 60
  let hour = minute * 60
  let day = hour * 24
  let halfamonth = day * 15
  let month = day * 30
  let now = new Date().getTime()
  let diffValue = now - dateTimeStamp
  if (diffValue < 0) {
    return
  }
  let monthC = diffValue / month
  let weekC = diffValue / (7 * day)
  let dayC = diffValue / day
  let hourC = diffValue / hour
  let minC = diffValue / minute
  if (monthC >= 1) {
    if (monthC <= 12) result = '' + parseInt(monthC) + '月前'
    else {
      result = '' + parseInt(monthC / 12) + '年前'
    }
  } else if (weekC >= 1) {
    result = '' + parseInt(weekC) + '周前'
  } else if (dayC >= 1) {
    result = '' + parseInt(dayC) + '天前'
  } else if (hourC >= 1) {
    result = '' + parseInt(hourC) + '小时前'
  } else if (minC >= 1) {
    result = '' + parseInt(minC) + '分钟前'
  } else {
    result = '刚刚'
  }
  return result
}

export const imFormatTime = date => {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()

  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

function formatNumber (n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
